/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.usecase.command;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.uml2.uml.Element;

import com.cea.papyrus.core.commands.StereotypePropertiesDisplayCreateCommand;
import com.cea.papyrus.core.helper.GraphEdgeHelper;
import com.cea.papyrus.diagraminterchange2.di2.Di2Factory;
import com.cea.papyrus.diagraminterchange2.di2.DiagramElement;
import com.cea.papyrus.diagraminterchange2.di2.GraphConnector;
import com.cea.papyrus.diagraminterchange2.di2.GraphEdge;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;

/**
 * Manages creation of extend conditions.
 * 
 * @author Gilles Cannenterre
 * @since 8 janv. 07
 */
public class ExtendConditionDisplayCreateCommand extends StereotypePropertiesDisplayCreateCommand {
	
	/** The uml child. */
	protected Element umlChild;
	
	public ExtendConditionDisplayCreateCommand(String name, DiagramElement semanticparent, DiagramElement parent, Element umlChild) {
		super(name, semanticparent, parent);
		this.umlChild = umlChild;
		setLabel("Display extend condition "+name);
	}

	public ExtendConditionDisplayCreateCommand(DiagramElement semanticparent, DiagramElement parent, Element umlChild) {
		super(semanticparent, parent);
		this.umlChild = umlChild;
		setLabel("Display extend condition "+name);
	}

	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.common.commands.StereotypePropertiesDisplayCreateCommand#createGraphNode()
	 */
	@Override
	protected GraphNode createGraphNode(){
		return factory.createGraphElement(umlChild,null,null,null);
	}

	/**
	 * This method tests if the graphNode is allowed to be created.
	 * 
	 * @return 
	 */
	@Override
	public boolean canExecute() {
		return true; 
	}


	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#execute()
	 */
	@Override
	public void execute() {
		if (canExecute()) {
			propertiesGraphNode = createGraphNode();
			// propertiesGraphNode.setSemanticParent(semanticParent);
			
			// Set the size of the extend condition
			propertiesGraphNode.setSize(new Dimension(250,50));
			
			
			Point anchorSourcePoint = ((GraphConnector)((GraphEdge)semanticParent).getAnchor().get(0)).getPosition();
			propertiesGraphNode.setPosition(new Point(anchorSourcePoint.x+100, anchorSourcePoint.y-100));
			((GraphNode)parent).getContained().add(propertiesGraphNode);
		
	
			GraphEdgeHelper ge= new GraphEdgeHelper();
			GraphEdge edge = (GraphEdge)ge.createGraphElement(null, (GraphElement)parent, null, null);
			
			Point sourcePos = new Point(propertiesGraphNode.getPosition().x+(propertiesGraphNode.getSize().width/2), 
					propertiesGraphNode.getPosition().y+propertiesGraphNode.getSize().height);
			
			anchorSourcePoint = ((GraphConnector)((GraphEdge)semanticParent).getAnchor().get(0)).getPosition();
			Point anchorTargetPoint = ((GraphConnector)((GraphEdge)semanticParent).getAnchor().get(1)).getPosition();
			Point targetPos = middlePoint(anchorSourcePoint, anchorTargetPoint); 	
			
			// create source anchor/connector
			GraphConnector srcConnector = Di2Factory.eINSTANCE.createGraphConnector();
			srcConnector.setPosition(targetPos);
			// create target connector
			GraphConnector targetConnector = Di2Factory.eINSTANCE.createGraphConnector();
			targetConnector.setPosition(sourcePos);
			// connect to graphEdge [0]=source  [1]=target
			edge.getAnchor().add(srcConnector);
			edge.getAnchor().add(targetConnector);
			
			getSourceAnchor(edge).setGraphElement((GraphElement)semanticParent);
			getTargetAnchor(edge).setGraphElement(propertiesGraphNode);
			edge.setContainer((GraphElement)parent);
		}
		
	}
}